<?php

namespace App\Http\Resources\Course;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

class CourseDetailsResource extends JsonResource
{
    public function toArray($request)
    {
        $video = $this->video();
        $comment = $this->comment();

        return [
            'id'             => $this->id,
            'name'           => $this->name,
            'class_id_1'     => $this->class_id_1,
            'class_name_1'   => $this->class1 ? $this->class1->name ?: '' : '',
            'class_id_2'     => $this->class_id_2,
            'class_name_2'   => $this->class2 ? $this->class2->name ?: '' : '',
            'cover'          => $this->cover,
            'banners'        => explode(',', $this->banners),
            'details'        => $this->details,
            'watch_num'      => $this->watch_num,
            'is_collect'     => $this->is_collect ?? false,
            'is_pay'         => $this->is_pay,
            'pay_amount'     => fen_to_yuan($this->pay_amount),
            'vip_is_pay'     => $this->vip_is_pay,
            'vip_pay_amount' => fen_to_yuan($this->vip_pay_amount),
            'show_pay'       => $this->show_pay,
            'member_info'    => $this->member_info ?? (object)[],
            'video_num'      => count($video),
            'video'          => $video,
            'comment_num'    => count($comment),
            'comment'        => $comment
        ];
    }

    private function video()
    {
        $data = [];

        foreach ($this->video ?: [] as $item) {
            if ($item->source && $item->source->status) {

                $history = $this->watch_history ? $this->watch_history->where('course_video_id', $item->id)->first() : null;

                $data[] = [
                    'id'         => $item->id,
                    'course_id'  => $item->course_id,
                    'source_id'  => $item->course_source_id,
                    'name'       => $item->source->name,
                    'cover'      => $item->source->cover,
//                    'file'      => $this->show_pay ? '' : $item->source->file,
                    'is_watch'   => (bool)$history && !$this->show_pay,
                    'pace'       => $history ? $history->pace : 0,
                    'state_desc' => (function () {
                        if ($this->is_pay) {
                            if ($this->member_info) {
                                if ($this->member_info['is_buy']) {
                                    $text = '已购买|is_buy';
                                } else {
                                    $text = $this->member_info['is_vip'] && !$this->vip_is_pay ? '会员专享|vip_free' : '解锁查看|wait_unlock';
                                }
                            } else {
                                $text = '解锁查看|wait_unlock';
                            }
                        } else {
                            $text = '免费|free';
                        }

                        [$text, $code] = explode('|', $text);

                        return compact('code', 'text');
                    })(),
                ];
            }
        }

        return $data;
    }

    private function comment()
    {
        $data = [];

        foreach ($this->comment ?: [] as $item) {
            $data[] = [
                'member_id'     => $item->member->id,
                'member_name'   => $item->member->nickname,
                'member_avatar' => $item->member->avatar,
                'content'       => $item->content,
                'status'        => $item->status,
                'created_at'    => Carbon::parse($this->created_at)->format('Y-m-d H:i:s')
            ];
        }

        return $data;
    }
}
